<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 12/10/18
 * Time: 4:47 PM
 */

class RedisWrapper
{
    const HOST = '127.0.0.1';
    const PORT = 6379;
    const AUTH_PASSWORD = '';

    /**
     * Used to store and provide instance for the getInstance() method
     */
    private static $instance;

    /**
     * Hidden constructor, only callable from within this class
     */
    private function __construct() { }

    /**
     * Create an instance if it hasn't been already
     * Then return the instance of this class
     * @return \Redis Object
     */
    public static function getInstance() {

        if(!self::$instance || !self::$instance instanceof \Redis) {
            // instance doesn't exist yet, so create it
            self::$instance = new \Redis();
            self::$instance->pconnect(self::HOST, self::PORT);
            if (self::AUTH_PASSWORD) {
                self::$instance->auth(self::AUTH_PASSWORD);
            }
            self::$instance->select(0);
        }

        // return an instance of this class (Database)
        return self::$instance;
    }

    /**
     * Hidden magic clone method, make sure an instance this class
     * cannot be cloned using the clone keyword
     */
    private function __clone() { }
}